﻿using DBUtil;
using Function;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission.Store
{
    public class GroupDAL
    {
        DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHStore);
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[Group]");
            strSql.Append(" where Name=@Name");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", name)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name, int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[Group]");
            strSql.Append(" where Name=@Name and GroupID<>@id");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", name),
                    new SqlParameter("@id", id),
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GroupEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Permission].[Group](");
            strSql.Append("GroupTypeID,Name,SuperiorID,Sort,Description,State)");
            strSql.Append(" values (");
            strSql.Append("@GroupTypeID,@Name,@SuperiorID,@Sort,@Description,@State)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupTypeID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,30),
					new SqlParameter("@SuperiorID", SqlDbType.Int,4),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4)};
            parameters[0].Value = model.GroupTypeID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.SuperiorID;
            parameters[3].Value = model.Sort;
            parameters[4].Value = model.Description;
            parameters[5].Value = model.State;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GroupEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Permission].[Group] set ");
            strSql.Append("GroupTypeID=@GroupTypeID,");
            strSql.Append("Name=@Name,");
            strSql.Append("SuperiorID=@SuperiorID,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("Description=@Description,");
            strSql.Append("State=@State");
            strSql.Append(" where GroupID=@GroupID");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupTypeID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,30),
					new SqlParameter("@SuperiorID", SqlDbType.Int,4),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4)};
            parameters[0].Value = model.GroupTypeID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.SuperiorID;
            parameters[3].Value = model.Sort;
            parameters[4].Value = model.Description;
            parameters[5].Value = model.State;
            parameters[6].Value = model.GroupID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int GroupID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[Group] ");
            strSql.Append(" where GroupID=@GroupID");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupID", SqlDbType.Int,4)
			};
            parameters[0].Value = GroupID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string GroupIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[Group] ");
            strSql.Append(" where GroupID in (" + GroupIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GroupEntity GetModel(int GroupID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 GroupID,GroupTypeID,Name,SuperiorID,Sort,Description,State from [Permission].[Group] ");
            strSql.Append(" where GroupID=@GroupID");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupID", SqlDbType.Int,4)
			};
            parameters[0].Value = GroupID;

            GroupEntity model = new GroupEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GroupEntity DataRowToModel(DataRow row)
        {
            GroupEntity model = new GroupEntity();
            if (row != null)
            {
                if (row["GroupID"] != null && row["GroupID"].ToString() != "")
                {
                    model.GroupID = int.Parse(row["GroupID"].ToString());
                }
                if (row["GroupTypeID"] != null && row["GroupTypeID"].ToString() != "")
                {
                    model.GroupTypeID = int.Parse(row["GroupTypeID"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["SuperiorID"] != null && row["SuperiorID"].ToString() != "")
                {
                    model.SuperiorID = int.Parse(row["SuperiorID"].ToString());
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["Description"] != null)
                {
                    model.Description = row["Description"].ToString();
                }
                if (row["State"] != null && row["State"].ToString() != "")
                {
                    model.State = int.Parse(row["State"].ToString());
                }
            }
            return model;
        }
    }
}
